﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PepsiCo.Business;

namespace PepsiCo.UI
{
    public partial class Login : BasePage
    {

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (CurUser != null)
                RedirectToPage(DefaultURL);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                HttpCookie cookies = Request.Cookies[_COOKIES.LOGIN];
                if (cookies != null)
                {
                    if (cookies.Values[_COOKIES.USERNAME] != string.Empty)
                    {
                        txtUserName.Text = Request.Cookies[_COOKIES.LOGIN][_COOKIES.USERNAME];
                        try
                        {
                            txtPassword.Attributes["value"] = Request.Cookies[_COOKIES.LOGIN][_COOKIES.PASSWORD].ToRsaDecrypt(Server.MapPath("~/BasePage/privatekey.xml"));
                        }
                        catch { }
                        RememberMe.Checked = true;
                        LoginButton.Focus();
                    }
                }
                else
                {
                    txtUserName.Focus();
                }
            }
        }

        protected override bool RequiredLogin()
        {
            return false;
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            pepsi_User user = DB.pepsi_Users.FirstOrDefault(p => p.Name == txtUserName.Text && p.Pwd == txtPassword.Text.Trim().ToMD5UTF8Encryption());
            if (user != null)
            {
                if (user.IsDeleted)
                {
                    lbMessage.Text = "Invalid user name or password!";
                    return;
                }
                if (user.IsLockedOut)
                {
                    lbMessage.Text = "This account is locked out!";
                    return;
                }
                HttpCookie cookies;
                if (RememberMe.Checked)
                {
                    cookies = Request.Cookies[_COOKIES.LOGIN];
                    if (cookies == null)
                    {
                        cookies = new HttpCookie(_COOKIES.LOGIN);
                    }
                    cookies.Values[_COOKIES.USERNAME] = txtUserName.Text.Trim();
                    try
                    {
                        cookies.Values[_COOKIES.PASSWORD] = txtPassword.Text.ToRsaEncrypt(Server.MapPath("~/BasePage/publickey.xml"));
                    }
                    catch { }
                    cookies.Expires = DateTime.Now.AddMonths(1);
                    Response.Cookies.Add(cookies);
                }
                else
                {
                    cookies = Request.Cookies[_COOKIES.LOGIN];
                    if (cookies != null)
                    {
                        cookies.Values[_COOKIES.USERNAME] = string.Empty;
                        Response.Cookies.Add(cookies);
                    }
                }
                CurUser = user;
                user.LastLoginDate = DateTime.Now;
                DB.SubmitChanges();
                if (!string.IsNullOrEmpty(ReturnURL))
                {
                    RedirectToPage(ReturnURL);
                }
                else
                {
                    RedirectToPage(DefaultURL);
                }
            }
            else
            {
                lbMessage.Text = "Invalid user name or password!";
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Invalid user name or password!')", true);
            }
        }
    }
}
